//
//  LGLoginManager.m
//  GameDemo
//
//  Created by Happy on 1/16/17.
//  Copyright © 2017 Happy. All rights reserved.
//

#import "LGLoginManager.h"
#import "LGThirdDelegate.h"
#import "LGLocalData.h"

typedef void(^ZBLoginCallBackType)(NSError *error, NSDictionary *result);

@interface LGLoginManager () {
    
}
@property (nonatomic, copy) ZBLoginCallBackType callBack;
@end


@implementation LGLoginManager

static LGLoginManager *_manager;
+ (instancetype)sharedInstance {
    if (!_manager) {
        _manager = [[LGLoginManager alloc] init];
        
        [LGThirdDelegate sharedInstance].loginCallback = ^(NSError *error, NSDictionary *result, LGLoginType loginType) {
            
            NSMutableDictionary *newResult = result.mutableCopy;
            newResult[@"appid"] = [self p_loginSDKAppId:loginType];
            error ? _manager.callBack(error, nil):_manager.callBack(nil, newResult);
        };
        
    }
    
    return _manager;
}

- (void)loginInWeiXin:(void(^)(NSError *error, NSDictionary *result))callback {
#if ZBLoginWeiXinIsOpen
    if (![WXApi isWXAppInstalled]) {
        NSError *noInstallError = [NSError errorWithDomain:@"com.zbsdk.login" code:-2 userInfo:@{NSLocalizedDescriptionKey:@"您还没有安装微信客户端"}];
        callback(noInstallError, nil);
        return;
    }
    if (self.weiXinAppId.length == 0) {
        NSError *weiXinError = [NSError errorWithDomain:@"com.zbsdk.login" code:-1 userInfo:@{NSLocalizedDescriptionKey:@"缺少参数微信appid"}];
        callback(weiXinError, nil);
        return;
    }
    _callBack = callback;
    
    
    SendAuthReq* req =[[SendAuthReq alloc] init];
    req.scope = @"snsapi_userinfo" ;
    req.state = @"6d4647b70fdf8592c7580218ffde00be" ;
    
    [WXApi sendReq:req];
    
#else
    NSLog(@"ZCSDK 微信登录功能暂未开发");
    
#endif
}

+ (NSString *)p_loginSDKAppId:(LGLoginType)loginType {
    switch (loginType) {
            case ZBLoginTypeWeiXin:
            return _manager.weiXinAppId;
            break;
    }
}
@end
